package com.inossem.warehousestorage_inossem.activity.out_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.outStorage.AllocatingOutStorageItemListAdapter;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageAllocatingOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.outStorage.OutStorageAllocatingOverviewRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.List;

/**
 * 作者：wsy on 2017/12/5 09:37
 * <p>
 * 邮箱：445979770@qq.com
 */

public class OutStorageAllocatingOverviewActivity extends BaseActivity {
    private TextView title, nextStep;

    private PullToRefreshLayout refresh;

    private PullableListView allocatingOutStorageItemList;

    private List<OutStorageAllocatingOverviewBean.BodyBean.ItemListBean> allData;

    private AllocatingOutStorageItemListAdapter adapter;

    private String mOutStorageAllocatingNumber, mMaterielCode, mTransferNumber, mDeliveryNumber;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_out_storage_allocating_overview);
        initView(OutStorageAllocatingOverviewActivity.this);
        SPUtils.removeAllocatingOutStorageInformation(OutStorageAllocatingOverviewActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(OutStorageAllocatingOverviewActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        allocatingOutStorageItemList = (PullableListView) activity.findViewById(R.id.allocatingOutStorageItemList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.allocatingOutStorageItemListRefresh);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
    }

    private void initData(Activity activity) {
        title.setText(getResources().getString(R.string.activity_out_storage_allocating_add_operator_allocating_an_overview));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_in_storage_receive_list_title_short), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        allocatingOutStorageItemList.setPullUpFlag(false);
        allocatingOutStorageItemList.setPullDownFlag(false);
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (check(activity)) {
                    Intent intent = new Intent(activity, OutStorageAllocatingAddOperatorActivity.class);
                    startActivity(intent);
                }
            }
        });
        mMaterielCode = "";
        getExtra(activity);
        getData(activity);
        setmScanListener((String scanResult, int length) -> {
            mMaterielCode = QRCodeUtils.DecryptQRCode(scanResult);
            getData(activity);
        }, QRCodeConstant.TYPE_MATERIEL_CODE);
    }

    private boolean check(Context context) {
        int confirmNumber = OutStorageUtils.getAllocatingOutStorageNumber(context);
        int allNumber = OutStorageUtils.getOutStorageAllocatingOverviewBean(context).getBody().getItem_list().size();
        if (confirmNumber != allNumber) {
            Utils.middleToast(context, getResources().getString(R.string.activity_out_storage_allocating_overview_must_all_send));
            return false;
        }
        return true;
    }

    private void getExtra(Activity activity) {
//        mOutStorageAllocatingNumber = activity.getIntent().getStringExtra(OutStorageExtra.OUT_STORAGE_PICKING_NUMBER);
        mTransferNumber = activity.getIntent().getStringExtra(InStorageExtra.TRANSFER_NUMBER);
        mDeliveryNumber = activity.getIntent().getStringExtra(InStorageExtra.DELIVERY_NUMBER);
    }

    private void addOutStoragePickingItemListData(Context context, OutStorageAllocatingOverviewBean.BodyBean bean, List<OutStorageAllocatingOverviewBean.BodyBean.ItemListBean> receiveInStorageItemList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new AllocatingOutStorageItemListAdapter(context, bean, receiveInStorageItemList, mMaterielCode, (String materielCode) -> {
                mMaterielCode = materielCode;
                getData(context);
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context) {
        String allocatingOutStorageInformation = SPUtils.getAllocatingOutStorageInformation(context);
        if (TextUtils.isEmpty(allocatingOutStorageInformation)) {
            OutStorageAllocatingOverviewRequest.request(context, mTransferNumber, mDeliveryNumber, new GsonBeanListener<OutStorageAllocatingOverviewBean>() {
                @Override
                public void onSuccess(OutStorageAllocatingOverviewBean bean) {
                    Gson gson = new Gson();
                    String json = gson.toJson(bean, OutStorageAllocatingOverviewBean.class);
                    SPUtils.saveAllocatingOutStorageInformation(context, json);
                    OutStorageAllocatingOverviewBean.BodyBean data = bean.getBody();
                    allData = bean.getBody().getItem_list();
                    addOutStoragePickingItemListData(context, data, ActivityUtils.getShowData(allData, 0), allocatingOutStorageItemList, true);
                }
            });
        } else {
            Gson gson = new Gson();
            OutStorageAllocatingOverviewBean bean = gson.fromJson(allocatingOutStorageInformation, OutStorageAllocatingOverviewBean.class);
            if (TextUtils.isEmpty(mMaterielCode)) {
                allData = bean.getBody().getItem_list();
            } else {
                allData = OutStorageUtils.getAllocatingOutStorageList(context, mMaterielCode);
            }
            addOutStoragePickingItemListData(context, bean.getBody(), ActivityUtils.getShowData(allData, 0), allocatingOutStorageItemList, true);
        }
    }

    public interface OutStorageAllocatingOverviewListener {
        void onNoDoubleClickListener(String materielCode);
    }

    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(OutStorageAllocatingOverviewActivity.this, getResources().getString(R.string.activity_storage_manage_clear_prompt), new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                OutStorageAllocatingOverviewActivity.super.onBackPressed();
            }
        }).show();
    }
}
